﻿using System;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetExp.MediaXenter.Database;

namespace NetExp.MediaXenter.Utilities
{
    public class LocalizationHelper
    {
        public static string GetLocalizationString(XmlDocument documento, string text)
        {
            string valore = text;

            IEnumerator numerator = documento.ChildNodes.GetEnumerator();

            while (numerator.MoveNext())
            {
                XmlLinkedNode nodo = (System.Xml.XmlLinkedNode)numerator.Current;
                if (nodo.Name == "Localization")
                {
                    foreach (XmlNode elemento in nodo.ChildNodes)
                    {
                        if (!(elemento is XmlComment))
                        {
                            if (elemento.Attributes["Text"].Value == text)
                            {
                                valore = elemento.Attributes["Value"].Value;
                                break;
                            }
                        }
                    }
                    if (valore != string.Empty)
                        break;

                }

            }

            return valore;
        }

        public static string GetOriginalString(XmlDocument documento, string text, string startWith)
        {
            string valore = text;

            IEnumerator numerator = documento.ChildNodes.GetEnumerator();

            while (numerator.MoveNext())
            {
                XmlLinkedNode nodo = (System.Xml.XmlLinkedNode)numerator.Current;
                if (nodo.Name == "Localization")
                {
                    foreach (XmlNode elemento in nodo.ChildNodes)
                    {
                        if (!(elemento is XmlComment))
                        {
                            if (elemento.Attributes["Text"].Value.StartsWith(startWith))
                            {
                                if (elemento.Attributes["Value"].Value == text)
                                {
                                    valore = elemento.Attributes["Text"].Value;
                                    break;
                                }
                            }
                        }
                    }
                    if (valore != string.Empty)
                        break;

                }

            }

            return valore;
        }

        public static XmlDocument GetLocalizationDocument(SettingsDS settingsDS, string path)
        {
            XmlDocument documento = new XmlDocument();
            documento.Load(path + "Localization\\" + settingsDS.General[0].Language + ".xml");
            return documento;
        }

        public static string TranslateString(SettingsDS _settingsDS, string text)
        {
            XmlDocument documento = GetLocalizationDocument(_settingsDS, Constants.LOCALIZATIONPATH);
            return GetLocalizationString(documento, text);
        }

        public static string OriginalString(SettingsDS _settingsDS, string text, string startWith)
        {
            XmlDocument documento = GetLocalizationDocument(_settingsDS, Constants.LOCALIZATIONPATH);
            return GetOriginalString(documento, text, startWith);
        }

    }
}
